package cn.iocoder.yudao.module.mqtt.service.config;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.mqtt.dal.dataobject.mqtt.MqttConfigDO;
import cn.iocoder.yudao.module.mqtt.framework.mqtt.MqttInstanceManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.mqtt.controller.admin.config.vo.*;
import cn.iocoder.yudao.module.mqtt.dal.dataobject.config.MqttConfig2DO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.mqtt.dal.mysql.config.MqttConfig2Mapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.mqtt.enums.ErrorCodeConstants.*;

/**
 * MQTT配置 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class MqttConfig2ServiceImpl implements MqttConfig2Service {

    @Resource
    private MqttConfig2Mapper config2Mapper;


    @Autowired
    private MqttInstanceManager mqttInstanceManager;

    @Override
    public Long createConfig(ConfigSaveReqVO createReqVO) {
        // 插入
        MqttConfig2DO config = BeanUtils.toBean(createReqVO, MqttConfig2DO.class);
        config2Mapper.insert(config);
        // 返回
        return config.getId();
    }

    @Override
    public void updateConfig(ConfigSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigExists(updateReqVO.getId());
        // 更新
        MqttConfig2DO updateObj = BeanUtils.toBean(updateReqVO, MqttConfig2DO.class);
        config2Mapper.updateById(updateObj);


        if(!updateObj.getEnabled()){
            mqttInstanceManager.stopMqttInstance(updateObj.getId());
        }

        if(updateObj.getEnabled()){
            MqttConfig2DO mqttConfig2DO = config2Mapper.selectById(updateReqVO.getId());

            MqttConfigDO bean = BeanUtils.toBean(mqttConfig2DO, MqttConfigDO.class);
            mqttInstanceManager.startMqttInstance(bean);
        }
    }

    @Override
    public void deleteConfig(Long id) {
        // 校验存在
        validateConfigExists(id);
        // 删除
        config2Mapper.deleteById(id);
    }

    @Override
        public void deleteConfigListByIds(List<Long> ids) {
        // 校验存在
        validateConfigExists(ids);
        // 删除
        config2Mapper.deleteByIds(ids);
        }

    private void validateConfigExists(List<Long> ids) {
        List<MqttConfig2DO> list = config2Mapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONFIG_NOT_EXISTS);
        }
    }

    private void validateConfigExists(Long id) {
        if (config2Mapper.selectById(id) == null) {
            throw exception(CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public MqttConfig2DO getConfig(Long id) {
        return config2Mapper.selectById(id);
    }

    @Override
    public PageResult<MqttConfig2DO> getConfigPage(ConfigPageReqVO pageReqVO) {
        return config2Mapper.selectPage(pageReqVO);
    }

}